package per.ethan.cloud.consumer.service.impl;

import cn.hutool.json.JSONUtil;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import per.ethan.cloud.common.mapper.BaseMapper;
import per.ethan.cloud.common.service.impl.BaseServiceImpl;
import per.ethan.cloud.consumer.entity.Balance;
import per.ethan.cloud.consumer.mapper.BalanceMapper;
import per.ethan.cloud.consumer.service.BalanceService;
import per.ethan.cloud.consumer.service.ProviderBalanceService;

import java.util.Map;

/**
 * @author Ethan
 * @date 2020/04/13 01:38
 */
@Slf4j
@Service("balanceService")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BalanceServiceImpl extends BaseServiceImpl<Balance> implements BalanceService {

    @Qualifier("balanceMapper")
    private final BalanceMapper balanceMapper;

    private final ProviderBalanceService providerBalanceService;

    @Override
    public BaseMapper<Balance> getBaseMapper() {
        return balanceMapper;
    }

    @Override
    // @Transactional(rollbackFor = Exception.class)
    @GlobalTransactional(name = "test_balance", rollbackFor = Exception.class)
    public boolean handlerMoney(Map<String, Object> map) {
        // 扣钱
        Balance zhangsan = JSONUtil.toBean(JSONUtil.toJsonStr(map.get("zhangsan")), Balance.class);
        balanceMapper.update(zhangsan);

        // 加钱
        Balance lisi = JSONUtil.toBean(JSONUtil.toJsonStr(map.get("lisi")), Balance.class);
        providerBalanceService.modify(lisi);

        return true;
    }

}
